/******************************************************************************
* @file    PT32X005_pwm.h
* @author  应用开发团队
* @version V1.1.0
* @date    2020/1/27
* @brief    This file contains all the functions prototypes for the PWM firmware library.
*
******************************************************************************
* @attention
*
*
*****************************************************************************/


/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef PT32X005_PWM_H
#define PT32X005_PWM_H

#ifdef __cplusplus
extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include "PT32X005.h"

/** @addtogroup PWM
  * @{
  */

/* Exported types ------------------------------------------------------------*/


/** @brief Definition of PWM timebase init structure. **/
typedef struct
{
	u16	CounterReload;         /*!< Period (Value for MR0 register)             */
	u8	ClockSel;              /*!< Counting Clock select:HCLK or PCLK          */
	u8	Prescaler;             /*!< Prescaler (Value for PR register)           */
	u8	CounterMode;           /*!< Counter mode up-counting or down-counting   */
} PWM_TimeBaseInitTypeDef;


/** @brief  PWM Init Structure definition **/
#define PCLK		((u32)0x00000000)
#define HCLK		((u32)0x00000004)

#define BKISel_GPIO		((u32)0x00000000)
#define BKISel_LOCKUP	((u32)0x00000010)
#define BKISel_HCLK_Fail	((u32)0x00000020)
#define BKISel_PLL_Fail	((u32)0x00000030)
#define BKISel_COMP0	((u32)0x00000040)
#define BKISel_COMP1	((u32)0x00000050)
#define BKISel_LVD		((u32)0x00000060)

#define CAPTURE_FE	((u32)0x00000000)
#define CAPTURE_RE	((u32)0x00000001)


#define CAPTURE_RISEEDAGE_F	((u32)0x00000001)
#define CAPTURE_FALLEDAGE_F	((u32)0x00000002)

/** @brief Enumeration of PWM channel. **/
typedef enum
{
	PWM_CH_1 = 0,                /*!< PWM channel 1 */
	PWM_CH_2,                    /*!< PWM channel 2 */
	PWM_CH_3,                    /*!< PWM channel 3 */
	PWM_CH_4                     /*!< PWM channel 4 */
} PWM_CH_Enum;

/** @brief Enumeration of PWM channel output mode. **/
typedef enum
{
	PWM_OCMode_Inactive   = 0,                    /*!< PWM channel no output */
	PWM_OCMode_PWM1,                      /*!< PWM channel output high when tc <= compare value,output low when tc > compare value */
	PWM_OCMode_PWM2,                      /*!< PWM channel output low when tc <= compare value,output high when tc > compare value */
	PWM_OCMode_Toggle                       /*!< PWM channel output reverse when tc match compare value */
} PWM_OM_Enum;

/** @brief Definition of PWM channel output init structure. **/
typedef struct
{
	PWM_CH_Enum	PWM_OCCh;              /*!< Channel selection refer to \ref PWM_CH_Enum */
	PWM_OM_Enum	PWM_OCMode;           /*!< Channel output mode selection refer to \ref MCTM_OM_Enum */
	u32	PWM_OutputNState;             /*!< CHxN output enble or disable refer to */
	u16	PWM_Pulse;         /*!< Value for MRx register */
	u32	PWM_OCPolarity;
	u32	PWM_OCNPolarity;
	u32	PWM_OCIdleState;
	u32	PWM_OCNIdleState;
	u8    DeathTime;            /*!< Value for DT register */
} PWM_OCInitTypeDef;


/** @brief Definition of PWM channel Capture init structure. **/
typedef struct
{
	PWM_CH_Enum	PWM_ICCh;              /*!< Channel selection refer to \ref PWM_CH_Enum */
	u16          PWM_ICRstCount;           /*!< Channel Capture mode selection*/
	u16          PWM_ICPolarity;
} PWM_ICInitTypeDef;

typedef struct
{
  u32 PWM_Break;
  u32 PWM_BreakPolarity;
  u32 PWM_BreakSource;
  u32 PWM_BreakControl;
} TIM_BKIInitTypeDef;


/* Exported constants --------------------------------------------------------*/
#define IS_PWM_ALL_PERIPH(PERIPH)		((PERIPH) == TIM1)

#define IS_PWM_REPEATTIME(TIMES)             ((TIMES) <= 0xF)

#define IS_PWM_COUNTER_RELOAD(RELOADVALUE)   ((RELOADVALUE) <= 0xFFFF)

#define IS_PWM_COUNTER_VALUE(COUNTERVALUE)   ((COUNTERVALUE) <= 0xFFFF)

#define IS_PWM_CLOCKSEL(SEL)	(((SEL) == PCLK) || \
								((SEL) == HCLK))

#define IS_PWM_BKISource_SEL(SEL)	(((SEL) == TIM1_CR1_BKIS_TIMx_BKIN) || \
									((SEL) == TIM1_CR1_BKIS_PLLCLKDISABLE)|| \
									((SEL) == TIM1_CR1_BKIS_PVD)|| \
									((SEL) == TIM1_CR1_BKIS_SOFT))

#define IS_PWM_BKIControl_SET(SET)	(((SET) == TIM1_Break_Idle) || \
                                   				((SET) == TIM1_Break_Close))

#define IS_PWM_BKI_SET(SET)	(((SET) == TIM1_Break_Enable) || \
                                   			((SET) == TIM1_Break_Disable))

#define IS_PWM_BKIPol_SET(SET)	(((SET) == TIM1_BreakPolarity_High) || \
                                        			((SET) == TIM1_BreakPolarity_Low))

#define IS_PWM_PRESCALER(PRESCALER)           ((PRESCALER) <= 0xFFFF)

#define PWM_CounterMode_Up			((u32)0x00000000)
#define PWM_CounterMode_Down			((u32)0x00000040)
#define PWM_CounterMode_CenterAligned	((u32)0x00000080)
#define IS_PWM_Conter_Mode(MODE)	(((MODE) == PWM_CounterMode_Up) || \
									((MODE) == PWM_CounterMode_Down)|| \
									((MODE) == PWM_CounterMode_CenterAligned))

#define PWM_EventSource_Update	TIM1_CR1_UG
#define PWM_EventSource_Break	TIM1_OCMR_SWBK
#define IS_PWM_EVENT_SOURCE(SEL) (((SEL) == PWM_EventSource_Update) || \
									((SEL) == PWM_EventSource_Break))

#define IS_PWM_CHCHANNEL(SEL)	(((SEL) == PWM_CH_1) || \
								((SEL) == PWM_CH_2) ||\
								((SEL) == PWM_CH_3) || \
								((SEL) == PWM_CH_4))

#define IS_PWM_OM(MODE)	(((MODE) == PWM_OCMode_Inactive)  || \
							((MODE) == PWM_OCMode_PWM1) || \
							((MODE) == PWM_OCMode_PWM2) || \
							((MODE) == PWM_OCMode_Toggle))

#define PWM_ICRstCount_Disable 	((u16)0x0000)
#define PWM_ICRstCount_Enable	TIM1_CAPR_IC1RC
#define IS_PWM_RC_SEL(SEL)	(((SEL) == PWM_ICRstCount_Disable)  || \
							((SEL) == PWM_ICRstCount_Enable))

#define PWM_ICPolarity_Rising		((u16)0x0001)
#define PWM_ICPolarity_Falling		((u16)0x0002)
#define PWM_ICPolarity_BothEdge		((u16)0x0003)
#define IS_PWM_POLARITY_SEL(SEL)	(((SEL) == PWM_ICPolarity_Rising)  || \
									((SEL) == PWM_ICPolarity_Falling) || \
									((SEL) == PWM_ICPolarity_BothEdge))

#define  PWM_OutputNState_Disable           ((u32)0x00000000)      /*!< PWM channel_N disable */
#define  PWM_OutputNState_Enable             TIM1_OCMR_OC1NE           /*!< PWM channel_N enable */
#define IS_PWM_CHNCTL(CHNCTL)	(((CHNCTL) == PWM_OutputNState_Disable) || \
 								((CHNCTL) == PWM_OutputNState_Enable))

#define PWM_OCPolarity_High	((u16)0x0000)
#define PWM_OCPolarity_Low	TIM1_OCMR_OC1P
#define IS_PWM_OC_POLARITY(POLARITY)	(((POLARITY) == PWM_OCPolarity_High) || \
                                      					((POLARITY) == PWM_OCPolarity_Low))

#define PWM_OCNPolarity_High	((u16)0x0000)
#define PWM_OCNPolarity_Low	TIM1_OCMR_OC1NP
#define IS_PWM_OCN_POLARITY(POLARITY) (((POLARITY) == PWM_OCNPolarity_High) || \
										((POLARITY) == PWM_OCNPolarity_Low))

#define PWM_OCIdleState_Low		((u16)0x0000)
#define PWM_OCIdleState_High		TIM1_OCMR_OIS1
#define IS_PWM_OCIDLE_STATE(STATE) (((STATE) == PWM_OCIdleState_Low) || \
									((STATE) == PWM_OCIdleState_High))

#define PWM_OCNIdleState_Low		((u16)0x0000)
#define PWM_OCNIdleState_High		TIM1_OCMR_OIS1N
#define IS_PWM_OCNIDLE_STATE(STATE)	(((STATE) == PWM_OCNIdleState_Low) || \
										((STATE) == PWM_OCNIdleState_High))


#define IS_PWM_COMPAREVALUE(VALUE)           ((VALUE) <= 0xFFFF)

#define IS_PWM_DEATHTIME(VALUE)              ((VALUE) <= 0xFF)

#define PWM_IT_ARI	TIM1_CR2_ARI
#define PWM_IT_OC1	TIM1_CR2_OC1I
#define PWM_IT_OC2	TIM1_CR2_OC2I
#define PWM_IT_OC3	TIM1_CR2_OC3I
#define PWM_IT_OC4	TIM1_CR2_OC4I
#define PWM_IT_IC1	((u32)0x00000020)	//Old IC1=OC3 Re Define
#define PWM_IT_IC2	TIM1_CAPR_IC2I
#define PWM_IT_IC3	TIM1_CAPR_IC3I
#define PWM_IT_IC4	TIM1_CAPR_IC4I
#define PWM_IT_BK	TIM1_OCMR_BKI

#define IS_PWM_IT_GET(GEL)	(((GEL) == PWM_IT_ARI)  || \
							((GEL) == PWM_IT_OC1)  || \
							((GEL) == PWM_IT_OC2)  || \
							((GEL) == PWM_IT_OC3)  || \
							((GEL) == PWM_IT_OC4)  || \
							((GEL) == PWM_IT_IC1)  || \
							((GEL) == PWM_IT_IC2)  || \
							((GEL) == PWM_IT_IC3)  || \
							((GEL) == PWM_IT_IC4)  || \
							((GEL) == PWM_IT_BK))

#define IS_PWM_FLAG_SET(SET) ((SET)  <= 0X3FFF)
#define IS_PWM_FLAG_GET(SEL)	(((SEL) == TIM1_SR_ARF)||\
								((SEL) == TIM1_SR_OC1F)     || \
								((SEL) == TIM1_SR_OC2F)     || \
								((SEL) == TIM1_SR_OC3F)     || \
								((SEL) == TIM1_SR_OC4F)     || \
								((SEL) == TIM1_SR_IC1F) || \
								((SEL) == TIM1_SR_IC1R) || \
								((SEL) == TIM1_SR_IC2F) || \
								((SEL) == TIM1_SR_IC2R) || \
								((SEL) == TIM1_SR_IC3F) || \
								((SEL) == TIM1_SR_IC3R) || \
								((SEL) == TIM1_SR_IC4F) || \
								((SEL) == TIM1_SR_IC4R) || \
								((SEL) == TIM1_SR_BIF))



/* Exported macro ------------------------------------------------------------*/
/* Exported functions ---------------------------------------------------------*/
void PMW_TimeBaseInit(TIM1_TypeDef* PWMx, PWM_TimeBaseInitTypeDef* TimeBaseInit);
void PWM_TimeBaseStructInit(PWM_TimeBaseInitTypeDef* TimeBaseInit);
void PWM_OCInit(TIM1_TypeDef* PWMx, PWM_OCInitTypeDef* OutInit);
void PWM_OCStructInit(PWM_OCInitTypeDef* PWM_OCInitStruct);
void PWM_ICInit(TIM1_TypeDef* PWMx, PWM_ICInitTypeDef* CapInit);
void PWM_ICStructInit(PWM_ICInitTypeDef* PWM_ICInitStruct);
void PWM_BKIConfig(TIM1_TypeDef* PWMx,TIM_BKIInitTypeDef* PWM_BKIInitStruct);
void PWM_BKIStructInit(TIM_BKIInitTypeDef* PWM_BKIInitStruct);

void PWM_ITRepeatConfig(TIM1_TypeDef* PWMx, u8 ITReload);
void PWM_PrescalerConfig(TIM1_TypeDef* PWMx, u16 Prescaler);
void PWM_SetAutoreload(TIM1_TypeDef* PWMx, u16 Reload);
void PWM_SetCounter(TIM1_TypeDef* PWMx, u16 Counter);
void PWM_CounterModeConfig(TIM1_TypeDef* PWMx, u32 CounterMode);
void PWM_GenerateEvent(TIM1_TypeDef* PWMx, u32 PWM_EventSource);
void PWM_Cmd(TIM1_TypeDef* PWMx, FunctionalState NewState);

void PWM_ITConfig(TIM1_TypeDef* PWMx, u32 PWM_INT, FunctionalState NewState);
void PWM_ClearFlag(TIM1_TypeDef* PWMx, u32 PWM_FLAG);
void PWM_SetCompare(TIM1_TypeDef* PWMx, u8 CHx, u16 Cmp);
FlagStatus PWM_GetFlagStatus(TIM1_TypeDef* PWMx, u32 PWM_FLAG);
u16 PWM_GetCapture(TIM1_TypeDef* PWMx, u8 CHx);
u16 PWM_GetCounter(TIM1_TypeDef* PWMx);
u16 PWM_GetPrescaler(TIM1_TypeDef* PWMx);
u8 PWM_GetITCounter(TIM1_TypeDef* PWMx);

/**
  * @}
  */


#ifdef __cplusplus
}
#endif

#endif


